/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-27
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-06
 * @Description: 
 */
import 'package:hive/hive.dart';

part 'conversation.g.dart'; // 引入生成的.g.dart文件

@HiveType(typeId: 3)
class Conversation {
  @HiveField(0)
  final String sessionObject; // 会话对象id

  @HiveField(1)
  final String sessionType; //会话对象类型,联系人/群聊

  @HiveField(2)
  final List<Message> messages; // 会话消息列表

  @HiveField(3)
  String lastMessage; // 最后一条消息的内容

  @HiveField(4)
  int unreadCount; // 未读消息数量

  @HiveField(5)
  DateTime lastActive; // 最后会话时间

  Conversation({
    required this.sessionObject,
    required this.sessionType,
    required this.messages,
    required this.lastMessage,
    required this.unreadCount,
    required this.lastActive,
  });

  void addMessage(Message message) {
    messages.add(message);
    lastMessage = message.content;
    lastActive = message.sentAt;
    if (message.sender != null) unreadCount += 1;
  }

  Conversation copyWith({
    String? sessionObject,
    String? sessionType,
    List<Message>? messages,
    String? lastMessage,
    int? unreadCount,
    DateTime? lastActive,
  }) {
    return Conversation(
      sessionObject: sessionObject ?? this.sessionObject,
      sessionType: sessionType ?? this.sessionType,
      messages: messages ?? this.messages,
      lastMessage: lastMessage ?? this.lastMessage,
      unreadCount: unreadCount ?? this.unreadCount,
      lastActive: lastActive ?? this.lastActive,
    );
  }
}

// 定义 Message 类，存储与该联系人/群组有关的所有聊天信息
@HiveType(typeId: 4)
class Message {
  @HiveField(0)
  final dynamic content; // 消息内容

  @HiveField(1)
  final String contentType; // 消息类型

  @HiveField(2)
  final DateTime sentAt; // 发送时间

  @HiveField(3)
  final String? sender; // null为我方发送，否则则为对方发送

  @HiveField(4)
  final bool isRead;

  Message({
    required this.content,
    required this.contentType,
    required this.sentAt,
    this.sender,
    required this.isRead,
  });
}
